/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.view.workers;

import java.awt.image.BufferedImage;
import javax.swing.SwingWorker;
import org.jfree.chart.ChartPanel;
import systole.view.charts.XYChart;
import systole.view.tabs.controllers.AnalysisController;
import systole.view.tabs.controllers.ResultsModel;

/**
 *
 * @author jmj
 */
public class WorkerReport extends SwingWorker<Void, Void> {

    private AnalysisController parentControl;
    private ChartPanel chartPanel;
    private boolean finished = false;
    private BufferedImage image;

    /**
     * @param analysisController
     */
    public WorkerReport(AnalysisController analysisController) {
        this.parentControl = analysisController;
    }

    @Override
    protected Void doInBackground() {
        try {
            ResultsModel modelResult = this.parentControl.getResultsModel();
            if ((modelResult != null) && (modelResult.getCurrentFinalSignal() != null)) {

                XYChart chart = new XYChart("Registro de Variación", "[ms]", "[%]");

                chart.addSeries("", modelResult.getCurrentFinalSignal().getFinalSegment().toDoubleArray(), this.parentControl.getAnalysis().getSignalFrequency().getFrequency().doubleValue());
                chart.setShowLegend(false);
                chart.setShowTitle(false);

                this.image = chart.plotThumbnail(280, 200, true);
                this.chartPanel = chart.blackAndWhitePlot();
                this.finished = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void done() {
        if (this.finished) {
            this.parentControl.getFrmAnalysis().getJReportView().getPnlReport().loadNewChart(chartPanel);
            this.parentControl.getFrmAnalysis().getJReportView().showReport();
            this.parentControl.getReportModel().updatePlotImage(this.image);

            if (this.parentControl.isWaitingForSave()) {
                this.parentControl.saveAfterProcess();
            }

        }
    }
}
